from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

# 指定chromedriver路径（137版本）
driver_path = "D:\\pycharm\\PM_test\\chromedriver.exe"

# 创建WebDriver实例
service = Service(driver_path)
options = Options()
browser = webdriver.Chrome(service=service, options=options)
browser.maximize_window()
wait = WebDriverWait(browser, 10)

# 登录函数
def login():
    browser.get("http://146.56.217.9:9131")
    browser.find_element(By.ID, "username").send_keys("admin_9131")
    browser.find_element(By.ID, "password").send_keys("admin")
    browser.find_element(By.CLASS_NAME, "button").click()

# 封装添加制造订单函数
def add_make_order(order_data):
    # 跳转至新制造订单界面
    browser.find_element(By.ID, "mainmenua_mrp").click()
    browser.find_element(By.CSS_SELECTOR, ".menu_contenu_mrp_mo_card .vsmenu").click()

    # # 物料清单
    browser.find_element(By.CSS_SELECTOR, f"select#fk_bom >option:nth-child({order_data['fk_bom']})").click()
    # 类型（只能2，3）
    browser.find_element(By.CSS_SELECTOR, f"select#mrptype >option:nth-child({order_data['mrptype']})").click()
    # 产品（只能2,3,4,5）
    browser.find_element(By.CSS_SELECTOR, f"select#fk_product >option:nth-child({order_data['fk_product']})").click()
    # 生产数量
    browser.find_element(By.ID, "qty").send_keys(str(order_data["qty"]))
    # 标签
    browser.find_element(By.ID, "label").send_keys(order_data["label"])
    # 生产仓库(2,3,4)
    browser.find_element(By.CSS_SELECTOR, f"select#fk_warehouse >option:nth-child({order_data['fk_warehouse']})").click()
    # 计划生产日期，计划结束日期
    browser.find_element(By.ID, "date_start_plannedButtonNow").click()  # 点击的是当前的时间
    browser.find_element(By.ID, "date_end_planned").send_keys(order_data["date_end_planned"])
    browser.find_element(By.CSS_SELECTOR, f"select#date_end_plannedhour >option:nth-child({order_data['date_end_plannedhour']})").click()
    browser.find_element(By.CSS_SELECTOR, f"select#date_end_plannedmin >option:nth-child({order_data['date_end_plannedmin']})").click()

    # 创建按钮（如果需要点击创建按钮，取消注释）
    browser.find_element(By.NAME, "add").click()

if __name__ == '__main__':
    try:
        login()
        sleep(1)  # 等待页面加载

        # 制造订单添加功能测试数据（3正向+3反向，共6个用例）
        # 测试结果：正向全部通过，反向最后一个找不到元素
        test_data = [
            # --------------------- 正向测试用例 ---------------------
            {
                "fk_bom": 2,  # 假设第二个BOM为手机主板
                "mrptype": 2,  # 生产类型
                "fk_product": 2,  # 第二个产品
                "qty": 100,
                "label": "手机主板生产订单-20250614",
                "fk_warehouse": 2,  # 第二个仓库
                "date_end_planned": "2025/07/15",
                "date_end_plannedhour": 15,  # 15:00
                "date_end_plannedmin": 1,  # 00分钟
                "expected_result": "制造订单添加成功，状态为'计划中'"
            },
            {
                "fk_bom": 3,  # 假设第三个BOM为电脑组装
                "mrptype": 3,  # 组装类型
                "fk_product": 4,  # 第四个产品
                "qty": 50,
                "label": "电脑组装订单-2025Q3",
                "fk_warehouse": 3,  # 第三个仓库
                "date_end_planned": "2025/08/30",
                "date_end_plannedhour": 18,  # 18:30
                "date_end_plannedmin": 7,  # 30分钟
                "expected_result": "制造订单添加成功，状态为'计划中'"
            },
            {
                "fk_bom": 2,
                "mrptype": 2,
                "fk_product": 2,
                "qty": 1,  # 最小数量
                "label": "样品生产订单-20250614",
                "fk_warehouse": 2,
                "date_end_planned": "2025/06/20",
                "date_end_plannedhour": 12,
                "date_end_plannedmin": 1,
                "expected_result": "制造订单添加成功，状态为'计划中'"
            },

            # --------------------- 反向测试用例 ---------------------
            {
                "fk_bom": 2,
                "mrptype": 2,
                "fk_product": 2,
                "qty": 0,  # 非法数量
                "label": "测试零数量订单",
                "fk_warehouse": 2,
                "date_end_planned": "2025/07/01",
                "date_end_plannedhour": 10,
                "date_end_plannedmin": 20,
                "expected_result": "提示'生产数量必须大于0'，订单未创建"
            },
            {
                "fk_bom": 2,
                "mrptype": 2,
                "fk_product": 2,
                "qty": 100,
                "label": "测试过去日期订单",
                "fk_warehouse": 2,
                "date_end_planned": "2025/01/01",  # 过去的日期
                "date_end_plannedhour": 10,
                "date_end_plannedmin": 20,
                "expected_result": "提示'计划结束日期不能早于当前日期'，订单未创建"
            } ,
            {
                "fk_bom": 99,  # 不存在的BOM
                "mrptype": 2,
                "fk_product": 2,
                "qty": 100,
                "label": "测试无效BOM",
                "fk_warehouse": 2,
                "date_end_planned": "2025/07/01",
                "date_end_plannedhour": 10,
                "date_end_plannedmin": 20,
                "expected_result": "提示'请选择有效物料清单'，订单未创建"
            }
        ]
        for data in test_data:
            add_make_order(data)
            sleep(1)  # 等待操作完成
    except Exception as e:
        print("发生错误：", e)
    finally:
        sleep(1)
        browser.quit()